#include "constraint.hh"

constraint::constraint (const std::string name, const bound& bnd)
  : name_ (name),
    bound_ (bnd)
{
  variables_ = std::vector<std::pair<variable, double> > ();
}

void
constraint::add_variable (const variable& var, double coeff)
{
  variables_.push_back (std::pair<variable, double> (var, coeff));
}

bound
constraint::get_bound () const
{
  return bound_;
}

double constraint::get_coeff(const variable& v) const
{
  std::vector<std::pair<variable, double> >::const_iterator it;

  it = variables_.begin();

  while ((it != variables_.end()) && (v != it->first))
    {
      ++it;
    }

  if (it != variables_.end())
    return it->second;
  else
    return 0;
}

std::string constraint::get_name() const
{
  return name_;
}

std::ostream& operator << (std::ostream& stream, const constraint& w)
{
  stream << w.name_ << '\t';
  std::vector<std::pair<variable, double> >::const_iterator it;

  w.bound_.print (stream);
  it = w.variables_.begin();

  while (it != w.variables_.end())
    {
      if (it->second != 1)
	stream << it->second << " * " << it->first;
      else
	stream << it->first;
      ++it;
      if (it != w.variables_.end())
	stream << " + ";
    }

  stream << " " << w.bound_;
  return stream;
}
